package com.ycdx.zwzc.action;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.ycdx.zwzc.impl.ReasonImpl;
import com.ycdx.zwzc.impl.RequestImpl;
import com.ycdx.zwzc.impl.ServiceTypeImpl;
import com.ycdx.zwzc.impl.SymptomImpl;
import com.ycdx.zwzc.pojo.Reason;
import com.ycdx.zwzc.pojo.Request;
import com.ycdx.zwzc.pojo.ServiceType;
import com.ycdx.zwzc.pojo.Symptom;
import com.ycdx.zwzc.utils.SimpleDateUtil;

public class RequestAction extends ActionSupport implements SessionAware,Preparable{

	private static final long serialVersionUID = -1377324475192364228L;
	private Map<String,Object> session;
	private List<ServiceType> allServiceType;
	private List<Symptom> allSymptom;
	private List<Reason> allReasons;
	private Request request;
	private Request tempReq = new Request();
	private List<Request> allRequests;
	private String id;
	private String from;
	private String serviceNum;
	
	public String getServiceNum() {
		return serviceNum;
	}
	public void setServiceNum(String serviceNum) {
		this.serviceNum = serviceNum;
	}
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}

	public List<Symptom> getAllSymptom() {
		return allSymptom;
	}
	public void setAllSymptom(List<Symptom> allSymptom) {
		this.allSymptom = allSymptom;
	}
	public List<Request> getAllRequests() {
		return allRequests;
	}
	public void setAllRequests(List<Request> allRequests) {
		this.allRequests = allRequests;
	}
	public Request getRequest() {
		return request;
	}
	public void setRequest(Request request) {
		this.request = request;
	}
	public List<Reason> getAllReasons() {
		return allReasons;
	}
	public void setAllReasons(List<Reason> allReasons) {
		this.allReasons = allReasons;
	}
	public List<ServiceType> getAllServiceType() {
		return allServiceType;
	}
	public void setAllServiceType(List<ServiceType> allServiceType) {
		this.allServiceType = allServiceType;
	}
	
	/**
	 * 需求输入页
	 */
	public String input(){
		return "requestInput";
	}
	
	/**
	 * 需求添加
	 * @return
	 */
	public String add(){
		//System.out.println(request.getOperator());
		//修改状态
		request.setStatus("<font color='#DAA520'><b>待处理</b></font>");
		//增加录入时间
		request = RequestImpl.getNewRequest(request);
		int flag = RequestImpl.addRequest(request);
		//System.out.println(request.getEditor());
		if(flag==1){
			//添加成功
			addActionMessage("需求添加成功！");
			return "unresolvedList";
		}else{
			//添加不成功
			addActionMessage("需求添加不成功，请重试！");
			return "requestInput";
		}
	}
	/**
	 * 删除指定id的需求
	 * @return
	 */
	public String delete(){
		int flag = RequestImpl.deleteRequestById(id);
		if(flag==1){
			//删除成功
			addActionMessage("删除成功！");
		}else{
			//删除不成功
			addActionMessage("归档不成功，请重试！");
		}
		if(from.equals("unresolved")){
			//来自待处理页面
			return "unresolvedList";
		}else if(from.equals("processed")){
			//来自已归档页面
			return "processedList";
		}
		return "allRequestsList";
	}
	/**
	 * 处理需求
	 */
	public String process(){
		request = RequestImpl.getRequestById(id);
		return "processRequestJSP";
	}
	
	
	/**
	 * 编辑需求，取得指定id的request对象
	 * @return
	 */
	public String edit(){
//		System.out.println(from);
//		System.out.println(id);
		request = RequestImpl.getRequestById(id);
		if(from.equals("unresolved")){
			//来自待处理页面
			return "requestEditInput";
		}else if(from.equals("processed")){
			//来自已归档页面
			return "processedRequestEditInput";
		}
		return "allRequestsList";
	}
	
	/**
	 * 处理需求，添加处理结果
	 * @return
	 */
	public String addResult(){	
//		System.out.println(request.getId());
//		System.out.println(request.getReason());
//		System.out.println(request.getResult());
		tempReq = RequestImpl.getRequestById(String.valueOf(request.getId()));
		tempReq.setReason(request.getReason());
		tempReq.setResult(request.getResult());
		tempReq.setStatus("<font color='#32CD32'><b>已处理</b></font>");
		int flag = RequestImpl.addResult(tempReq);
		//System.out.println(request.getEditor());
		if(flag==1){
			//添加成功
			addActionMessage("处理成功！");
			return "processedList";
		}else{
			//添加不成功
			addActionMessage("处理不成功，请重试！");
			return "requestInput";
		}
	}
	/**
	 * 将指定id的工单归档
	 * @return
	 */
	public String archive(){
		
		request = RequestImpl.getRequestById(id);
		request.setStatus("已归档");
		//添加归档时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(System.currentTimeMillis());
		request.setArchivedTime(time);
		//添加处理时长
		String time1 = request.getTime();
		//System.out.println(time1);
		long stratTime = SimpleDateUtil.convert2long(time1, "yyyy-MM-dd HH:mm:ss");
		//long stratTime = Time.valueOf(time1).getTime() ;
		long endTime = SimpleDateUtil.convert2long(time, "yyyy-MM-dd HH:mm:ss");
		request.setProcessTime(SimpleDateUtil.getProcessTime(endTime - stratTime));
		
		int flag = RequestImpl.archiveRequest(request);
		if(flag==1){
			//归档成功
			addActionMessage("归档成功！");
			//allRequests = RequestImpl.getAllArchivedRequests();			
		}else{
			//归档不成功
			addActionMessage("归档不成功，请重试！");
			//return "requestUnresolved";
		}
		//无论归档成功与否，都跳回已归档需求列表
		//System.out.println(1111111111);
		allRequests = RequestImpl.getAllArachivedRequests();
		return "listArchivedRequestJSP";
	}
	
	/**
	 * 查看指定id的需求
	 * @return
	 */
	public String view(){
		request = RequestImpl.getRequestById(id);
		return "viewRequest";
	}
	
	/**
	 * 更新需求，将更新后的request对象入库
	 * @return
	 */
	public String update(){
		//System.out.println(id);
		//request = RequestImpl.getRequestById(id);
		int flag = RequestImpl.updateRequest(request);
		if(flag==1){
			//修改成功
			addActionMessage("需求修改成功！");
			return "unresolvedList";
		}else{
			//修改不成功
			addActionMessage("需求修改不成功，请重试！");
			return "requestEditInput";
		}
	}
	
	/**
	 * 列表所有已处理需求
	 * @return
	 */
	public String listProcessed(){
		allRequests = RequestImpl.getAllProcessedRequests();
		return "listProcessedRequestJSP";
	}
	
	/**
	 * 列表所有待解决需求
	 * @return
	 */
	public String listUnresolved(){
		allRequests = RequestImpl.getAllUnresolvedRequests();
		return "listUnresolvedRequestJSP";
	}
	
	/**
	 * 列表所有已归档需求
	 * @return
	 */
	public String listArchived(){
		allRequests = RequestImpl.getAllArachivedRequests();
		return "listArchivedRequestJSP";
	}
	
	/**
	 * 列表所有需求
	 * @return
	 */
	public String listAllRequests(){
		allRequests = RequestImpl.getAllRequests();
		return "listAllRequestsJSP";
	}
	
	/**
	 * 根据业务号码查询
	 * @return
	 */
	public String query(){
		allRequests = RequestImpl.queryRequestByServiceNum(serviceNum);
		session.put("allRequests", allRequests);
		return "listQueryResult";
	}
	
	/**
	 * 初始化故障类型
	 */
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		allServiceType = ServiceTypeImpl.queryAllServiceType();
		allSymptom = SymptomImpl.queryAllSymptom();
		allReasons = ReasonImpl.queryAllReasons();
	}
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}
	
	
}
